#include <QTextEdit>
#include <QLabel>
#include <QPicture>
#include <QPushButton>
#include <iostream>
#include <QScrollArea>
#include <QFont>
#include <QThread>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ParametersControl.h"

#define MAXIMUM_NUMBER 4

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    this->ui->setupUi(this);
    this->parameter_ = new ParametersControl();
    this->createLayoutLeft();
    this->createLayoutRight();
}

void MainWindow::createLayoutLeft()
{
    QHBoxLayout *first = this->createHorizontalLayoutFactor(" ", false);
    QHBoxLayout *second = this->createHorizontalLayoutFactor("+", false);
    QHBoxLayout *third = this->createHorizontalLayoutFactor("=", false);
    
    QLabel *temp = new QLabel(this);
    temp->setFixedSize(210, 3);
    temp->setStyleSheet("background-color: grey; margin-left:50px;");
    
    this->bt_add_ = new QPushButton("Add Factor", this);
    this->bt_add_->setFixedWidth(110);
    this->bt_add_->setVisible(false);
    
    this->bt_del_ = new QPushButton("Delete Factor", this);
    this->bt_del_->setFixedWidth(110);
    this->bt_del_->setVisible(false);
    
    this->ui->resultLayout_->addWidget(temp);
    this->ui->resultLayout_->addLayout(third);
    this->ui->leftLayout_->addLayout(first);
    this->ui->leftLayout_->addLayout(second);
    this->ui->leftLayout_->setSpacing(10);
    
    QHBoxLayout *buttons = new QHBoxLayout();
    buttons->addWidget(this->bt_add_);
    buttons->addWidget(this->bt_del_);
    this->ui->leftLayout_->addLayout(buttons);
    
    QObject::connect(this->bt_add_, SIGNAL(clicked()), this, SLOT(addNewFactor()));
    QObject::connect(this->bt_del_, SIGNAL(clicked()), this, SLOT(deleteFactor()));
}

void MainWindow::runCalculation()
{
    std::vector<std::string> temp;
    for (std::vector<QTextEdit *>::iterator it = this->operation_.begin(); it != this->operation_.end();++it)
    {
        if ((*it)->toPlainText().toStdString().size() == 0)
        {
            std::cerr << "Some factors are empty, please fill it before runing the process !" << std::endl;
            this->parameter_->clear();
            return ;
        }
        temp.push_back((*it)->toPlainText().toStdString());
    }
    
    std::cout << "FACTORS : " << std::endl;

    for (int i = 0; i < temp.size(); i++)
        std::cout << temp[i] << std::endl;
    
    if (this->parameter_->checkParameters(temp) == false)
        return ;
    
    int number_line = this->parameter_->getParameters().size();
    this->calculator_ = new Calculator(this->parameter_->getParameters());
    this->calculator_->calculation();

    std::string *result_display = this->calculator_->getFirstResult();
    if (result_display != NULL)
    {
        std::cout << "NON NUL" << std::endl;
        this->bt_process_->setVisible(false);
        if (number_line == 3)
        {
            QFont serifFont("Times", 14, QFont::Bold);
            this->result_process_->setFont(serifFont);
        }
        if (number_line == 4 || number_line == 5)
        {
            QFont serifFont("Times", 12, QFont::Bold);
            this->result_process_->setFont(serifFont);
        }
        if (number_line >= 6)
        {
            QFont serifFont("Times", 10, QFont::Bold);
            this->result_process_->setFont(serifFont);
        }
        this->result_process_->setText(result_display->c_str());
        this->result_process_->setVisible(true);
        this->reset_->setVisible(true);
    }
    this->parameter_->clear();
    delete this->calculator_;
}

void MainWindow::createLayoutRight()
{
    QLabel *limitation = new QLabel(this);
    limitation->setFixedWidth(1);
    limitation->setStyleSheet("background-color:grey");
    limitation->setAlignment(Qt::AlignRight);
    this->ui->centralLayout_->addWidget(limitation);
    
    this->result_process_ = new QLabel(this);
    QFont serifFont("Times", 14, QFont::Bold);
    this->result_process_->setFont(serifFont);
    this->ui->rightLayout_->addWidget(this->result_process_);
    this->result_process_->setVisible(false);
    
    this->bt_process_ = new QPushButton("Process", this);
    this->bt_process_->setFixedSize(250, 30);
    this->ui->rightLayout_->setAlignment(Qt::AlignHCenter);
    this->ui->rightLayout_->addWidget(this->bt_process_);
    
    this->reset_ = new QPushButton("Reset", this);
    this->reset_->setFixedWidth(110);
    this->reset_->setVisible(false);
    QObject::connect(this->reset_, SIGNAL(clicked()), this, SLOT(ResetAll()));
    
    this->next_ = new QPushButton("Next", this);
    this->next_->setFixedWidth(110);
    this->next_->setVisible(false);
    
    this->ui->rightLayout_->addWidget(this->next_);
    this->ui->rightLayout_->addWidget(this->reset_);

    QObject::connect(this->bt_process_, SIGNAL(clicked()), this, SLOT(runCalculation()));
}

void MainWindow::addNewFactor()
{
    if (this->ui->leftLayout_->count() < MAXIMUM_NUMBER+1)
    {
        QHBoxLayout *second = this->createHorizontalLayoutFactor("+", true);
        this->ui->leftLayout_->insertLayout(this->ui->leftLayout_->count()-1, second);
    }
    if (this->ui->leftLayout_->count() >= 3)
    {
        this->bt_del_->setVisible(true);
    }
    if (this->ui->leftLayout_->count() == 3)
    {
        this->bt_del_->setVisible(false);
    }
}

void MainWindow::deleteFactor()
{
    std::cout << "Delete Factor" << std::endl;
    QLayoutItem *horizontal_temp = this->ui->leftLayout_->takeAt(this->ui->leftLayout_->count()-2);
    QLayout *horizontal_temp_layout = horizontal_temp->layout();
    
    if (horizontal_temp_layout != 0)
    {
        QLayoutItem *child;
        while ((child = horizontal_temp_layout->takeAt(0)) != 0)
        {
            QWidget *temp = child->widget();
            if (temp != 0)
            {
                std::cout << "Widgets found" << std::endl;
                temp->hide();
                temp->setParent(NULL);
            }
            delete child;
        }
    }
    
    this->operation_.erase(this->operation_.end()-1);
    this->factorLayouts_.erase(this->factorLayouts_.end()-1);
    
    if (this->ui->leftLayout_->count() >= 3)
    {
        this->bt_del_->setVisible(true);
    }
    if (this->ui->leftLayout_->count() == 3)
    {
        this->bt_del_->setVisible(false);
    }
}

void MainWindow::ResetAll()
{
    this->parameter_->clear();
    this->result_process_->setVisible(false);
    this->reset_->setVisible(false);
    this->next_->setVisible(false);
    this->bt_process_->setVisible(true);
    
    for (int i = 0; i < this->ui->leftLayout_->count()-3; i++)
        this->deleteFactor();
    
    for (std::vector<QTextEdit *>::iterator it = this->operation_.begin(); it != this->operation_.end();++it)
        (*it)->setText("");
}

QHBoxLayout* MainWindow::createHorizontalLayoutFactor(char *c, bool isAddedByUser)
{
    QHBoxLayout *second = new QHBoxLayout();
    QTextEdit *textFact2 = new QTextEdit();
    textFact2->setFixedSize(100, 20);
    QLabel *plus = new QLabel();
    plus->setFixedSize(10, 10);
    plus->setText(c);
    second->addWidget(plus);
    second->addWidget(textFact2);
    this->operation_.push_back(textFact2);
    this->factorLayouts_.push_back(second);
    return second;
}

MainWindow::~MainWindow()
{
    delete ui;
    delete this->bt_process_;
    delete this->bt_add_;
    delete this->bt_del_;
    delete this->reset_;
    delete this->next_;
    delete this->result_process_;
    delete this->parameter_;
}
